package com.bw.module_mine

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_storage.db.DBUtils
import com.bw.module_mine.databinding.ActivityMyCollectionsBinding
import com.bw.module_mine.model.Adpter.CollectionAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@AndroidEntryPoint
@Route(path = "/mine/MyCollectionsActivity")
class MyCollectionsActivity : AppCompatActivity() {
   private lateinit var binding: ActivityMyCollectionsBinding
   private lateinit var collectAdapter: CollectionAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMyCollectionsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.rvCollect.layoutManager = LinearLayoutManager(this)
        collectAdapter = CollectionAdapter()
        binding.rvCollect.adapter = collectAdapter

        loadAllCollect()
    }

    fun loadAllCollect() {
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                val collectList = DBUtils.getDB().videoStarDao().getAllFavorites()
                    .toMutableList()
                    .apply { reverse() }

                withContext(Dispatchers.Main){
                    collectAdapter.setData(collectList)
                }
            } catch (e:Exception){
                e.printStackTrace()
            }
        }
    }
}